# -*- coding: utf-8 -*-
# @Date   : 2019/12/10
# @File   : R14_EC_UnpaidBadsumNumber_WHB_2D.py
# @Author : breeze


'''eof
name:企业未结清不良类贷款笔数
code:R14_EC_UnpaidBadsumNumber_WHB_2D
tableName:
columnName:
groups:弱规则>企业征信校验
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''
import json
import sys
import pandas as pd
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def R14_EC_UnpaidBadsumNumber_WHB_2D():
    data = EC_DSJ_2D.get('data')
    factor = []
    flag = False
    # 借贷交易汇总信息 EBB[1,1]
    EBB = getTable(data, 'EBB', 1, 0)
    if EBB == [{}]:
        return u'缺失值'
    elif EBB == 0:
        return 0
    else:
        # 信贷交易汇总信息单元 EB02[0,1]
        EB02 = getTable(EBB[0], 'EB02', 0, 1)
        if EB02 == [{}]:
            return u'缺失值'
        elif EB02 == 0:
            return 0
        else:
            # 未结清信贷交易汇总信息段EB02A[1,1]
            EB02A = getTable(EB02[0], 'EB02A', 1, 1)
            if EB02A == [{}]:
                return u'缺失值'
            elif EB02A == 0:
                return 0
            else:
                # 其他信贷交易分类汇总信息 EB02AH[0,20]
                EB02AH = getTable(EB02A[0], 'EB02AH', 0, 1)
                if EB02AH == [{}]:
                    return u'缺失值'
                elif EB02AH == 0:
                    return 0
                else:
                    for i in EB02AH:
                        try:
                            typeList = ["1", "2", "3"]
                            # 业务类型
                            EB02AD01 = i.get('EB02AD01')
                            # 资产质量分类
                            EB02AD02 = i.get('EB02AD02')
                            # 账户数
                            EB02AS04 = i.get('EB02AS04')
                            # 余额
                            EB02AJ06 = i.get('EB02AJ06')
                            if EB02AD01 not in null_type_list and EB02AD02 not in null_type_list \
                                    and isfloat(EB02AS04) and isfloat(EB02AJ06):
                                flag = True
                                if EB02AD01 in typeList and EB02AD02 == "3" \
                                        and (float(EB02AS04) > 0 or float(EB02AJ06) > 0):
                                    factor.append(1)
                        except:
                            continue

    return sum(factor) if flag else u'缺失值'


if __name__ == '__main__':
    ratingRequest = {"relativeDate": "20191212"}
    file_obj = open(r'../data/WHB/R14_EC_UnpaidBadsumNumber_WHB/EC_DSJ_2D.json', 'r')
    content = file_obj.read()
    EC_DSJ_2D = json.loads(content, strict=False)
    aa = R14_EC_UnpaidBadsumNumber_WHB_2D()
    print aa

result = R14_EC_UnpaidBadsumNumber_WHB_2D()
